package ${controllerPackage!};

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.anosi.controller.BaseController;
import ${entityPackage};
import cn.anosi.page.PageResponseBean;
import cn.anosi.page.Pager;
import ${servicePackage};
import cn.anosi.utils.ExcelUtils;


@Controller
@RequestMapping("/${entity?uncap_first}")
public class ${entity}Controller extends BaseController{
	
	@Autowired
	protected ${entity}Service ${entity?uncap_first}Service; 
	
	/**
	 * 列表页面
	 * @return
	 */
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(){
		return "${entity?uncap_first}/list";
	}
	
	/**
	 * 列表数据
	 * @param pager
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	@ResponseBody
	public PageResponseBean<${entity}> list(Pager pager){
		return ${entity?uncap_first}Service.list(pager);
	}
	
	/**
	 * 根据id 锁定/启用 
	 * @param id
	 * @param locked
	 * @return
	 */
	@RequestMapping(value="/lock", method=RequestMethod.POST)
	@ResponseBody
	public int lock(Integer id, Integer status){
		return ${entity?uncap_first}Service.lock(id, status);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	@ResponseBody
	public int delete(Integer id){
		return ${entity?uncap_first}Service.delete(id);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/batchDel", method=RequestMethod.POST)
	@ResponseBody
	public int batchDel(@RequestBody List<Integer> ids){
		return ${entity?uncap_first}Service.batcahDel(ids);
	}
	
	/**
	 * 添加页面
	 * @return
	 */
	@RequestMapping(value = "/add", method=RequestMethod.GET)
	public String add(){
		return "${entity?uncap_first}/add";
	}
	
	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	@ResponseBody
	public int save(${entity} ${entity?uncap_first}){
		${entity?uncap_first}.setIsDel(0);
		${entity?uncap_first}.setStatus(0);
		${entity?uncap_first}.setCreateTime(new Date());
		try {
			${entity?uncap_first}Service.save(${entity?uncap_first});
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}
	
	/**
	 * 修改页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method=RequestMethod.GET)
	public String edit(Integer id, Model model){
		${entity} ${entity?uncap_first} = ${entity?uncap_first}Service.findOne(id);
		model.addAttribute("${entity?uncap_first}", ${entity?uncap_first});
		return "${entity?uncap_first}/edit";
	}
	
	/**
	 * 修改
	 * @param ${entity?uncap_first}
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/edit", method=RequestMethod.POST)
	@ResponseBody
	public int edit(${entity} ${entity?uncap_first}) throws IllegalAccessException, InvocationTargetException{
		if(null != ${entity?uncap_first} && null != ${entity?uncap_first}.getId()){
			${entity} entity = ${entity?uncap_first}Service.getById(${entity?uncap_first}.getId());
			<#list list as arr>
			entity.set${arr[1]?cap_first}(${entity?uncap_first}.get${arr[1]?cap_first}());
			</#list>
			entity.setEditTime(new Date());
			try {
				${entity?uncap_first}Service.save(entity);
				return 1;
			} catch (Exception e) {
				return 0;
			}
		}else{
			return 0;
		}
	}
	
	/**
	 * 导出
	 * @param entity
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/export", method=RequestMethod.GET)
	public void export(${entity} entity,HttpServletResponse response) throws IOException{
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();
		
		
		ArrayList<String> titleList = new ArrayList<String>();
		titleList.add("id");
		<#list list as arr>
		titleList.add("${arr[2]}");
		</#list>
		titleList.add("创建时间");
		titleList.add("修改时间");
		String[] title =  new String[titleList.size()];
		titleList.toArray(title);
		
		//标题行
		//String[] title = {"id",  "创建时间", "修改时间"};	
		ExcelUtils.setDefaultHead(workbook, sheet, title);
		
		//数据行
		List<${entity}> list = ${entity?uncap_first}Service.list();
		for (${entity} ${entity?uncap_first} : list) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(titleList.indexOf("id")).setCellValue(${entity?uncap_first}.getId());
			<#list list as arr>
			if(${entity?uncap_first}.get${arr[1]?cap_first}() != null){
				dataRow.createCell(titleList.indexOf("${arr[2]}")).setCellValue(${entity?uncap_first}.get${arr[1]?cap_first}());
				if(${entity?uncap_first}.get${arr[1]?cap_first}().toString().length() * 256 > sheet.getColumnWidth(titleList.indexOf("${arr[2]}"))){
					sheet.setColumnWidth(titleList.indexOf("${arr[2]}"), ${entity?uncap_first}.get${arr[1]?cap_first}().toString().length() * 256);
				}
			}
			</#list>
			if(${entity?uncap_first}.getCreateTime() != null){
				dataRow.createCell(titleList.indexOf("创建时间")).setCellValue(sdf.format(${entity?uncap_first}.getCreateTime()));
				sheet.setColumnWidth(titleList.indexOf("创建时间"), sdf.format(${entity?uncap_first}.getCreateTime()).getBytes().length * 256);
			}
			if(${entity?uncap_first}.getEditTime() != null){
				dataRow.createCell(titleList.indexOf("修改时间")).setCellValue(sdf.format(${entity?uncap_first}.getEditTime()));
				sheet.setColumnWidth(titleList.indexOf("修改时间"), sdf.format(${entity?uncap_first}.getEditTime()).getBytes().length * 256);
			}
		}
		
		String fileName = new String("${description}列表.xls".getBytes("GB2312"), "ISO_8859_1"); 
		response.setHeader("Content-Disposition", "attachment;filename="+ fileName );
		
		ServletOutputStream os = response.getOutputStream();
		workbook.write(os);
	}
	
	/**
	 * 导入
	 * @param file
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value = "/importXls", method=RequestMethod.POST)
	@ResponseBody
	public int importXls(@RequestParam MultipartFile file) throws IOException, ParseException{
		HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
		HSSFSheet sheet = workbook.getSheetAt(0);
		ArrayList<${entity}> list = new ArrayList<${entity}>();
		
		ArrayList<String> titleList = new ArrayList<String>();
		titleList.add("id");
		<#list list as arr>
		titleList.add("${arr[2]}");
		</#list>
		titleList.add("创建时间");
		titleList.add("修改时间");
		
		for (Row row : sheet) {
			int rowNum = row.getRowNum();
			if(rowNum == 0){
				continue;
			}
			
			${entity} ${entity?uncap_first} = new ${entity}();
			<#list list as arr>
			${entity?uncap_first}.set${arr[1]?cap_first}(row.getCell(titleList.indexOf("${arr[2]}")).getStringCellValue());
			</#list>
			//${entity?uncap_first}.setCreateTime(sdf.parse(row.getCell(titleList.indexOf("创建时间")).getStringCellValue()));
			${entity?uncap_first}.setStatus(0);
			${entity?uncap_first}.setIsDel(0);
			list.add(${entity?uncap_first});
		}
		if(list.size() > 0){
			return ${entity?uncap_first}Service.batchSave(list);
		}else{
			return 0;
		}
	}
	
}
